package com.example.ObserverPattern_EventBus;

import com.example.ObserverPattern_EventBus.service.UserService;
import com.google.common.eventbus.EventBus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <pre>
 * 功能描述：
 * </pre>
 *
 * @author: 李明杰
 * @date: 2022/12/12 21:05
 */
@RestController
@Slf4j
public class UserController {
    @Resource
    private UserService userService; // 依赖注入
    @Resource
    private EventBus asyncEventBus;


    public void setRegObservers(List<Object> observers) {
        for (Object observer : observers) {
            asyncEventBus.register(observer);
        }
    }

    public Long register(String telephone, String password) {
        //省略输入参数的校验代码
        //省略userService.register()异常的try-catch代码
        long userId = userService.register(telephone, password);

        asyncEventBus.post(userId);
        log.info("register success");
        return userId;
    }
}





